package com.dacool.http;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dacool.model.CartItem;

public class CreateOrderRequest extends BaseRequest {

	private ArrayList<CartItem> productList;

	private int addressID;

	private String payMethod;

	private int shippingID;
	
	private String remark;
	
	public CreateOrderRequest setRemark(String remark) {
		this.remark = remark;
		return this;
	}

	public CreateOrderRequest setShippingID(int id) {
		this.shippingID = id;
		return this;
	}

	public CreateOrderRequest setPayMethod(String payMethod) {
		this.payMethod = payMethod;
		return this;
	}

	public CreateOrderRequest setAddressID(int addressID) {
		this.addressID = addressID;
		return this;
	}

	public ArrayList<CartItem> getProductList() {
		return productList;
	}

	public CreateOrderRequest setProductList(ArrayList<CartItem> productList) {
		this.productList = productList;
		return this;
	}

	@Override
	protected String url() {
		// TODO Auto-generated method stub
		return "createOrder.jsn";
	}

	@Override
	protected void generateParams(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		JSONArray array = new JSONArray();
		for (CartItem item : productList) {
			JSONObject itemObject = new JSONObject();
			/**
			 * &shipping=快递&shippingId=1&shipCost=10&markText=9点到10点
			 */
			try {
				itemObject.put("goodsId", item.getProduct().getId());
				itemObject.put("nums", item.getAmount());

				array.put(itemObject);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		map.put("goodsList", array.toString());
		map.put("addressId", addressID);
		map.put("payMethod", payMethod);
		map.put("shipping", "0");
		map.put("shippingId", shippingID);
		map.put("markText", remark);
	}

}
